'use strict';

/**
 * @ngdoc overview
 * @name sourceApp
 * @description
 * # sourceApp
 *
 * Main module of the application.
 */
var myApp = angular
  .module('sourceApp', [
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.bootstrap'
  ])
  .config(function ($routeProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl'
      })
      .when('/login',{
        templateUrl: 'views/login.html',
        controller: 'LoginCtrl'
      })
      .when('/about', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl'
      })
      .when('/directory',{
        templateUrl: 'views/directory.html',
        controller: 'DirectoryCtrl'
      })
      .otherwise({
        redirectTo: '/'
      });
  });

myApp.run(['$rootScope', '$location', function ($rootScope, $location) {
    $rootScope.$on('$routeChangeStart', function (event) {
        if(!$rootScope.currentUser) {
            $location.path('/login');
        }
        if( $location.url() === '/login' ){
          // alguna validación extra ... 
        }
    });
}]);